// pages/community/community.js

var QQMapWX = require('../../qqmap-wx-jssdk.js');
var qqmapsdk;
const app=getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    province: '',
    city: '',
    latitude: '',
    longitude: '',
    local:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 实例化API核心类
    qqmapsdk = new QQMapWX({
      key: 'GNLBZ-ZYD66-FLGSF-MU5JJ-SMNVS-HUB3W'    // 必填
   });
   let vm = this;
   vm.getUserLocation();
  },
  getData:function(){
    let self = this
    console.log(self.data.local)
    wx.request({
      url: `${app.globalData.url}/community/city`, 
      data: {
        Wcity:self.data.local
      },
      method:'GET',
      header: {
        'content-type': 'application/json' 
      },
      success: function (res) {
        console.log(res.data)
        let result = res.data;
        if (result.length>0){
          for (var index in result) {
            if(result[index].Wzan>9999){
                  result[index].Wzan=parseInt(result[index].Wzan/10000)+'w+'
            }
          }
          self.setData({
            petsData:result,
          })
          
        }
      },
      
    })
    
  },
  getUserLocation: function () {
    let vm = this;
    wx.getSetting({
      success: (res) => {
        console.log(JSON.stringify(res))
        if (res.authSetting['scope.userLocation'] != undefined && res.authSetting['scope.userLocation'] != true) {
          wx.showModal({
            title: '请求授权当前位置',
            content: '需要获取您的地理位置，请确认授权',
            success: function (res) {
              if (res.cancel) {
                wx.showToast({
                  title: '拒绝授权',
                  icon: 'none',
                  duration: 1000
                })
              } else if (res.confirm) {
                wx.openSetting({
                  success: function (dataAu) {
                    if (dataAu.authSetting["scope.userLocation"] == true) {
                      wx.showToast({
                        title: '授权成功',
                        icon: 'success',
                        duration: 1000
                      })
                      //再次授权，调用wx.getLocation的API
                      vm.getLocation();
                    } else {
                      wx.showToast({
                        title: '授权失败',
                        icon: 'none',
                        duration: 1000
                      })
                    }
                  }
                })
              }
            }
          })
        } else if (res.authSetting['scope.userLocation'] == undefined) {
          //调用wx.getLocation的API
          vm.getLocation();
        }
        else {
          //调用wx.getLocation的API
          vm.getLocation();
        }
      }
    })
    
  },
  // 微信获得经纬度
  getLocation: function () {
    let vm = this;
    wx.getLocation({
      type: 'wgs84',
      success: function (res) {
        console.log(JSON.stringify(res))
        var latitude = res.latitude
        var longitude = res.longitude
        vm.getLocal(latitude, longitude)
      },
      fail: function (res) {
        console.log('fail' + JSON.stringify(res))
      }
    })
  },
  // 获取当前地理位置
  getLocal: function (latitude, longitude) {
    let vm = this;
    qqmapsdk.reverseGeocoder({
      location: {
        latitude: latitude,
        longitude: longitude
      },
      success: function (res) {
        console.log(JSON.stringify(res));
        let province = res.result.ad_info.province
        let city = res.result.ad_info.city
        let district = res.result.ad_info.district
        vm.setData({
          province: province,
          city: city,
          latitude: latitude,
          longitude: longitude,
          local:province+city+district
        }) 
        vm.getData()
      },

      fail: function (res) {
        console.log(res);
      },
      complete: function (res) {
        // console.log(res);
      }
    });
  },
  onShow: function () {
    this.getUserLocation()
  },
  //跳转到商品详情页
  jumpDetail: function (e) {
   let petsnum =e.currentTarget.id;
   console.log(petsnum)
    wx.navigateTo({
      url:"/pages/com_detail/com_detail?Wid= "+ petsnum
    })
  },
})